//
//  MCBillingInfoViewController.swift
//  StoreManage
//
//  Created by MC on 2018/8/10.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCBillingInfoViewController: MCViewController {

    private var type = 1    // 类型 1全部；2自营；3非自营
    private var pageNum = 1
    private var endTime = ""
    private var startTime = ""

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        managerClosure()
        
        sendNetworking()
    }
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        //        tableView.backgroundColor = UIColor.MCGray_light
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var topView: MCBillingInfoHeadView = {
        let view = MCBillingInfoHeadView()
        return view
    }()
}

extension MCBillingInfoViewController : UITableViewDelegate,UITableViewDataSource {
    //MARK: - System Delegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCBillingInfoCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.backgroundColor = UIColor.MCGray_light
        cell.model = self.dataArrayM[indexPath.row] as! MCBillingInfoModel
        
        
        return cell
    }
}

//MARK: 通知回调，闭包回调，点击事件
extension MCBillingInfoViewController {
    func managerClosure() {
        
        weak var weakSelf = self
        
        let titleArray = ["全部收入","自营收入","非自营收入"]
        
        
        topView.categoryClosure = { () -> Void in
            
            UIAlertController.showActionSheet(vc: self, message: "选择查询范围", items: titleArray, custom: { (index, item) in
             
                self.topView.chooseButton.nameLabel.text = item
                self.type = index + 1
                self.pageNum = 1
                self.sendNetworking()
            })
        }
        
        
        topView.timeClosure = { () -> Void in
            let vc = MCTimeRangeViewController()
            
            vc.closure = { (start,end) in
                
                self.startTime = start.MCString_prefix(index: 10)
                self.endTime = end.MCString_prefix(index: 10)
                
                
                if self.startTime.count + self.endTime.count == 0 {
                    self.topView.categoryLabel.text = "全部"
                } else {
                    self.topView.categoryLabel.text = self.startTime + "~" + self.endTime
                }
                
                
                self.pageNum = 1
                self.sendNetworking()
            }
            
            MCPushToViewController(current: weakSelf!, target: vc, isHiddenBottomBarWhenBack: true)
        }
    }
    
    // MARK: - Action
    @objc func rightItemClicked() {
        let vc = MCReportFormsPageViewController()
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }

}


//MARK: 网络请求
extension MCBillingInfoViewController {
    func sendNetworking() {
        sendNetworking_list()
        sendNetworking_title()
    }
    
    func sendNetworking_title() {
        let para = [
            "endTime" : endTime,
            "startTime" : startTime,
            "type"    : type
            ] as [String : Any]
        
        MCNetwork.POST(JAVA_getOrderStatisticsTitle, para, vc: self, success: { (data) in
            let dataDict = data.dictionaryObject ?? [:]

            self.topView.dataDict = dataDict
            
        }, failure: nil)
    }
    
    func sendNetworking_list() {
        let para = [
            "type" : type,
            "endTime" : endTime,
            "startTime" : startTime,
            "pageNum" : pageNum,
            "pageSize" : 20
            ] as [String : Any]
        
        MCNetwork.POST(JAVA_getOrderStatisticsInfo, para, vc: self, success: { (data) in
            
            let dataArray = data.arrayObject ?? []

            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            if self.pageNum == 1 {
                self.dataArrayM.removeAllObjects()
                self.tableView.mj_footer.resetNoMoreData()
            }
            
            
            if dataArray.count == 0 {
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }
            
            
            if dataArray.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                
                for dict in dataArray {
                    let model = MCBillingInfoModel()
                    model.setValuesForKeys(dict as! [String : Any])
                    self.dataArrayM.add(model)
                }
            }
            self.tableView.reloadData()
            
        }, failure: nil)
    }

}

//MARK: UI的处理,通知的接收
extension MCBillingInfoViewController {
    func baseSetting() {
        self.title = "账单信息"
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("销售报表", target: self, selector: #selector(rightItemClicked))
        
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNum = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNum += 1
            self.sendNetworking()
        })
        
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
        
        tableView.tableHeaderView = topView
    }

}

